#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ==================================================
# @File    : assert_control
# @IDE     : PyCharm
# @Author  : liuxiangui
# @Date    : 2023/11/26 18:27
# @Describe: 
# ==================================================
import types
from typing import Text, Dict, Callable, Any
from library.assertion import assert_functions
from library.log import logger


class AssertDriver:
    @classmethod
    def assert_driver(cls, check_value: Any, asser_type: Text, expect_value: Any, message: Text = ""):
        """
        断言驱动
        :param check_value: 检查值
        :param asser_type:
        :param expect_value: 预期值
        :param message: 断言失败报错
        :return:
        """
        type_dict = {
            "==": "equals",
            "<": 'less_than',
            "<=": 'less_than_or_equals',
            ">": 'greater_than',
            ">=": 'greater_than_or_equals',
            "!=": 'not_equals',
            "contains": 'contains',
            "in": 'contains_by'
        }
        assert_mapping = cls.load_module_functions(assert_functions)
        real_type = type_dict.get(asser_type)
        logger.info(f"{check_value} {real_type} {expect_value}", attach_name="检查点")
        assert_mapping[real_type](check_value, expect_value, message)

    @classmethod
    def load_module_functions(cls, module) -> Dict[Text, Callable]:
        """
        加载模块，获取所有方法名称
        :param module: 模块名称
        :return: Dict
        """
        # 获取 断言类型模块中方法的名称和所在的内存地址
        module_functions = {}
        for name, item in vars(module).items():
            if isinstance(item, types.FunctionType):
                module_functions[name] = item
        return module_functions
